# -*- coding:utf-8 -*-
#作者：琴弦上的宇宙
#时间：2021年10月22日

import os
from common import *
from openCv import *
from myGui import *
from monitor import *

class scSingleHand(myScene):
    def __init__(self,bValid):
        self.bValid=bValid
        self.tagName=[]
        self.funcName=[]
        self.name='scSingleHand'
        self.path='resource/mercenary/base/scSingleHand'
        self.tagPng=[myPng(self.path,png) for png in os.listdir(self.path) if png.startswith('tag')]
        self.funcPng=[myPng(self.path,png) for png in os.listdir(self.path) if png.startswith('func')]
        self.setFunc([])

    def resetName(self,newName):
        self.name=newName

    def enable(self):
        self.bValid=True

    def dis(self):
        self.bValid=False

    def setFunc(self,funcList):
        self.tagName.clear()
        self.funcName.clear()
        if len(funcList)>0:
            for name in funcList:
                self.tagName.append(('tag'+str(name)))
                self.funcName.append(('func'+str(name)))
        else:
            self.tagName.append('tagFightRound')
            self.tagName.append('tagFightBuff')
            self.tagName.append('tagSurpriseNewTask')
            self.tagName.append('tagFightQuitAward')
            self.tagName.append('tagSurpriseAward')
            self.tagName.append('tagHeroUp')
            self.tagName.append('tagGetTaskAward')
            self.tagName.append('tagHeroChip')
            self.tagName.append('tagHeroNewEquip')
            self.tagName.append('tagHeroNewSkill')
            self.tagName.append('tagHeroFull')
            self.tagName.append('tagStoneUp')
            self.tagName.append('tagDayNewTask')
            self.tagName.append('tagLoseBox')
            self.funcName.append('funcFightRound')
            self.funcName.append('funcFightBuff')
            self.funcName.append('funcSurpriseNewTask')
            self.funcName.append('funcFightQuitAward')
            self.funcName.append('funcSurpriseAward')
            self.funcName.append('funcHeroUp')
            self.funcName.append('funcGetTaskAward')
            self.funcName.append('funcHeroChip')
            self.funcName.append('funcHeroNewEquip')
            self.funcName.append('funcHeroNewSkill')
            self.funcName.append('funcHeroFull')
            self.funcName.append('funcStoneUp')
            self.funcName.append('funcDayNewTask')
            self.funcName.append('funcLoseBox')

    def isOwn(self,background):
        if self.bValid:
            for tag in self.tagPng:
                for name in self.tagName:
                    if (tag.name==name):
                        bFind,x,y,w,h=bFindInBackground(background,tag,0.80)
                        if bFind:
                            return True
        else:
            return False

    def proc(self,background):

        #循环
        bg=background
        while True:

            #寻找
            for func in self.funcPng:
                for name in self.funcName:
                    if func.name==name:
                        bFind,x,y,w,h=bFindInBackground(bg,func,0.80)
                        if bFind:
                            if func.name=='funcFightRound':
                                self.resetName('scSingleHand_FightRound')
                                moveAndClick(x+w/2,y+h/2,5)
                                return True
                            elif func.name=='funcFightBuff':
                                self.resetName('scSingleHand_FightBuff')
                                moveAndClick(x+w/2,y+h/2,2)
                                return True
                            elif func.name=='funcSurpriseNewTask':
                                self.resetName('scSingleHand_SurpriseNewTask')
                                moveAndClick(x+w/2,y+h/2)
                                bg=SaveScreen()
                                if not self.isOwn(bg):
                                    MyGui.gSurpriseTaskCnt+=1
                                return True
                            elif func.name=='funcFightQuitAward':
                                SaveAwardPng()
                                self.resetName('scSingleHand_FightQuitAward')
                                moveAndClick(x+w/2,y+h/2,2)
                                bg=SaveScreen()
                                if not self.isOwn(bg):
                                    MyGui.gQuitCnt+=1
                                return True
                            elif func.name=='funcSurpriseAward':
                                SaveAwardPng()
                                self.resetName('scSingleHand_SurpriseAward')
                                moveAndClick(x+w/2,y+h/2,2)
                                bg=SaveScreen()
                                if not self.isOwn(bg):
                                    MyGui.gSurpriseOverCnt+=1
                                return True
                            elif func.name=='funcGetTaskAward':
                                self.resetName('scSingleHand_GetTaskAward')
                                moveAndClick(x+w/2,y+h/2)
                                return True
                            elif func.name=='funcHeroChip':
                                self.resetName('scSingleHand_HeroChip')
                                moveAndClick(x+w/2,y+h/2,2)
                                return True
                            elif func.name=='funcHeroNewEquip':
                                self.resetName('scSingleHand_HeroNewEquip')
                                moveAndClick(x+w/2,y+h/2,2)
                                return True
                            elif func.name=='funcHeroNewSkill':
                                self.resetName('scSingleHand_HeroNewSkill')
                                moveAndClick(x+w/2,y+h/2,2)
                                return True
                            elif func.name=='funcHeroFull':
                                self.resetName('scSingleHand_HeroFull')
                                moveAndClick(x+w/2,y+h/2+200,2)
                                return True
                            elif func.name=='funcStoneUp':
                                self.resetName('scSingleHand_StoneUp')
                                moveAndClick(x+w/2,y+h/2,2)
                                return True
                            elif func.name=='funcHeroUp':
                                self.resetName('scSingleHand_HeroUp')
                                moveAndClick(x+w/2,y+h/2+150,2)
                                return True
                            elif func.name=='funcDayNewTask':
                                self.resetName('scSingleHand_DayNewTask')
                                moveAndClick(x+w/2,y+h/2)
                                return True
                            elif func.name=='funcLoseBox':
                                self.resetName('scSingleHand_LoseBox')
                                moveAndClick(x+w/2,y+h/2)
                                return True

            #重新截图判断
            bg=SaveScreen()
            if not self.isOwn(bg):
                return False
